package org.tigris.atlas.xml.schema.mda.generator;

import java.util.Iterator;

import org.apache.velocity.VelocityContext;
import org.tigris.atlas.mda.generator.AbstractResourcesGenerator;
import org.tigris.atlas.mda.generator.GenerationContext;
import org.tigris.atlas.mda.generator.GenerationException;
import org.tigris.atlas.mda.metadata.MetadataRepository;
import org.tigris.atlas.mda.metadata.element.Composite;
import org.tigris.atlas.xml.schema.mda.element.SchemaComposite;
import org.tigris.atlas.xml.schema.mda.element.SchemaElementUtils;

public class SchemaCompositeGenerator extends AbstractResourcesGenerator {

	public void generate(GenerationContext context) throws GenerationException {
		String applicationName = context.getArtifactId();
		Iterator composites = MetadataRepository.getInstance().getAllComposites(applicationName).values().iterator();
		
		String fileName;
		String basefileName = context.getOutputFile();		
		basefileName = replaceBasePackage(basefileName, context.getBasePackageAsPath());
		while (composites.hasNext()) {
			Composite composite = (Composite) composites.next();
			SchemaComposite schemaComposite = new SchemaComposite(composite);
			
			VelocityContext vc = new VelocityContext();
			vc.put("composite", schemaComposite);
			vc.put("basePackage", context.getBasePackage());			
			
			String baseNamespace = SchemaElementUtils.translateBasePackageToBaseSchemaLocation(context.getBasePackage());
			vc.put("baseNamespace", baseNamespace);
			
			fileName = replaceCompositeName(basefileName, composite.getType());
			context.setOutputFile(fileName);
			
			generateFile(context, vc);
		}
	}

}
